const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const globalConfig = require('./../config');

module.exports = function(calcConfig) {
    const devMode = process.env.NODE_ENV === 'development',
        cssLoader = [ devMode ? 'style-loader': MiniCssExtractPlugin.loader, 'css-loader?modules', 'postcss-loader', 'sass-loader', {
            loader: 'sass-resources-loader',
            options: {
                resources: [
                    path.join(calcConfig.srcPath, 'common/styles', '_variable.scss'),
                    path.join(calcConfig.srcPath, 'common/styles', '_mixins.scss')
                ]
            }
        }],
        plugins = [],
        dllPath = path.join(calcConfig.basePath, 'dll', devMode ? 'react_dll_dev': 'react_dll');

    if( process.env.NODE_ENV !== 'development' ) {
        plugins.push(
            new CopyWebpackPlugin({
                patterns: [{
                    from: path.join(dllPath, 'react.dll.js'),
                    to: `${globalConfig.prefix.js}/[name].[ext]`,
                    force: true
                }]
            })
        );
    }

    plugins.push(new webpack.DllReferencePlugin({
        context: dllPath,
        manifest: path.join(dllPath, 'react.manifest.json')
    }));

    plugins.push(new webpack.WatchIgnorePlugin([/\.d\.ts$/]))

    return {
        module: {
            rules: [{
                test: /\.s?css$/,
                use: cssLoader
            }]
        },
        resolve: {    
            extensions: ['.jsx', '.tsx', '.ts']
        },
        plugins
    };
}